#ifndef __THREAD_HPP__
#define __THREAD_HPP__ 

#pragma once

#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>
#include <unistd.h>
namespace ThreadModule
{

    using func_t = std::function<void(const std::string& name)>;

    class Thread 
    {
     public:
        Thread(func_t func, const std::string name = "none-name")
            :_func(func), _threadName(name), _stop(true)
        {}

        void Excute()
        {
            _func(_threadName);
        }

        static void* threadRoutine(void* args) 
        {
            Thread* self = static_cast<Thread*>(args);
            self->Excute();
            return nullptr;
        }

        bool Start()
        {
            int n = pthread_create(&_tid, nullptr, threadRoutine, this);
            if(!n)
            {
                _stop = false;
                return true;
            }
            else 
                return false;
        }

        void Detach()
        {
            if(!_stop)
                pthread_detach(_tid);
        }

        void Join()
        {
            if(!_stop)
                pthread_join(_tid, nullptr);
        }

        const std::string& name()
        {
            return _threadName;
        }

        ~Thread()
        {}
     private:
         pthread_t _tid;
         std::string _threadName;
         func_t _func;
         bool _stop;
    };
}

#endif 
